package com.example.search.controller;

import com.example.search.model.Goods;
import com.example.search.service.IGoodsService;
import com.example.search.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Optional;
import java.util.UUID;

/**
 * Created by liulanhua on 2018/9/25.
 */
@Api(value = "商品接口")
@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    IGoodsService goodsService;


    @ApiOperation(value = "根据id查询商品信息", notes="根据id查询商品信息", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "String", paramType = "query", example = "d63b7330b7fd443380326d78ad3966f6")
    @GetMapping(value = "/findById")
    public ResponseEntity<Optional<Goods>> findById(@RequestParam("id") String id) {
        Optional<Goods> data = goodsService.findById(id);
        return new ResponseEntity<>(data, HttpStatus.OK);
    }

    @ApiOperation(value = "根据店铺编码查询商品信息", notes="根据店铺编码查询商品信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopCode", value = "编码", required = true, dataType = "String", paramType = "query", example = "000001"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "int", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = true, dataType = "int", paramType = "query", example = "10")
    })
    @GetMapping(value = "/findByShopCode")
    public ResponseEntity<Page<Goods>> findByShopCode(@RequestParam("shopCode") String shopCode,
            @RequestParam("page") int page, @RequestParam("size") int size) {
        Pageable pageable = PageRequest.of(page, size, Sort.by("createTime"));
        Page<Goods> data = goodsService.findByShopCode(shopCode, pageable);
        return new ResponseEntity<>(data, HttpStatus.OK);
    }


    @ApiOperation(value = "新增商品信息", notes="新增商品信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsName", value = "商品名称", required = true, dataType = "String", paramType = "query", example = "商品名称"),
            @ApiImplicitParam(name = "shopCode", value = "店铺编码", required = true, dataType = "String", paramType = "query", example = "000001"),
            @ApiImplicitParam(name = "price", value = "售价", required = true, dataType = "int", paramType = "query", example = "10.5"),
            @ApiImplicitParam(name = "desc", value = "描述", required = true, dataType = "String", paramType = "query", example = "描述")
    })
    @PostMapping(value = "/createGoods", produces = {"application/json"}, consumes = {"application/json"})
    public ResponseEntity<Goods> createGoods(@RequestBody Goods goods) {
        String id = UUID.randomUUID().toString().replace("-", "");
        String createTime = DateUtil.date2Str(new Date());
        goods.setId(id);
        goods.setCreateTime(createTime);
        Goods data = goodsService.save(goods);
        return new ResponseEntity<>(data, HttpStatus.OK);
    }


}
